<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select value="" placeholder="请选择" v-model="levelOneId" @change="getlevelTwoList" :disabled="show">
          <el-option :label="item.name" :value="item.id" v-for="item in levelOneList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select value="" placeholder="请选择" v-model="levelTwoId" @change="getlevelThreeList" :disabled="show">
          <el-option :label="item.name" :value="item.id" v-for="item in levelTwoList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select value="" placeholder="请选择" v-model="levelThreeId" @change="getAttrInfoId" :disabled="show">
          <el-option :label="item.name" :value="item.id" v-for="item in levelThreeList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props:['show'],
  data() {
    return {
      levelOneList: [],
      levelTwoList: [],
      levelThreeList: [],
      levelOneId: "",
      levelTwoId: "",
      levelThreeId: "",
    };
  },
  created() {
    this.getlevelOneList();
  },
  methods: {
    // 获取一级分类列表
    async getlevelOneList() {
      const result = await this.$API.attr.reqGetCategorylevelOne();
      if (result.code == 200) {
        this.levelOneList = result.data;
      }
    },
    //获取二级分类列表 当一级分类下拉框发生变动时触发
    async getlevelTwoList() {
      this.levelTwoList = [];
      this.levelThreeList = [];
      this.levelTwoId = "";
      this.levelThreeId = "";
      const result = await this.$API.attr.reqGetCategorylevelTwo(this.levelOneId);
      if (result.code == 200) {
        this.levelTwoList = result.data;
      }
    },
    // 获取三级分类列表 当二级分类下拉框发生变动时触发
    async getlevelThreeList() {
      this.levelThreeList = [];
      this.levelThreeId = "";
      const result = await this.$API.attr.reqGetCategorylevelThree(
        this.levelTwoId
      );
      if (result.code == 200) {
        this.levelThreeList = result.data;
      }
    },
    //获取attrInfoId
    getAttrInfoId() {
      let { levelOneId, levelTwoId, levelThreeId } = this;
      this.$store.dispatch("attr/getAttrInfoId", {
        levelOneId,
        levelTwoId,
        levelThreeId,
      });
    },
  },
};
</script>